<?php
//https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/phpquery/phpQuery-0.9.5.386-onefile.zip
include('../lib/phpQuery-onefile.php');

$data_json=<<<json_str
{
    "HasMore": false,
    "Resources": [
//         {
//             "Title": "[210106]アプリゲーム『CUE!』AiRBLUE 4th Single「最高の魔法」[DVD付初回限定盘][320K]",
//             "TypeId": 43,
//             "TypeName": "音乐/动漫音乐",
//             "SubgroupId": 390,
//             "SubgroupName": "天使动漫论坛",
//             "Magnet": "magnet:?xt=urn:btih:PYCEIAMYL6RGZARU4L3SWJE3CIEQNE7W",
//             "PageUrl": "https://share.dmhy.org/topics/view/556234_210106_CUE%21_AiRBLUE_4th_Single_DVD_320K.html",
//             "FileSize": "87.5MB",
//             "PublishDate": "2020-01-05 16:15:00"
//         }
    ]
}
json_str;


/*
http://example.com/list?keyword={keyword}&subgroup={subgroupId}&type={typeId}&r={random}
http://localhost/dandan/dmhy/list?keyword=fate&subgroup=185&type=2&r=12345
=>
https://share.dmhy.org/topics/list/page/1?keyword={keyword}&sort_id={typeId}&team_id={subgroupId}&order=date-desc
*/

$params=array(
   "host_uri"=>"https://share.dmhy.org",
    "keyword"=>$_REQUEST["keyword"],
    "subgroup"=>$_REQUEST["subgroup"],
    "type"=>$_REQUEST["type"],
    "r"=>$_REQUEST["r"],
);
$params["list_uri"]="{$params['host_uri']}/topics/list/page/1?keyword={$params['keyword']}&sort_id={$params['type']}&team_id={$params['subgroup']}&order=date-desc";
// var_dump($params);


$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $params["list_uri"],
    CURLOPT_RETURNTRANSFER => true,
//     CURLOPT_ENCODING => "utf-8",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_SSL_VERIFYPEER=>false,
    CURLOPT_SSL_VERIFYHOST=>false,
    //CURLOPT_POSTFIELDS => json_encode($data_array),
    CURLOPT_HTTPHEADER => array(
        "Content-type:text/html;charset='utf-8'",
        "Accept:text/html",
        "Cache-Control:no-cache",
        "Pragma:no-cache",
    ),
));


$response_text = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
    return $data_json;
} 
// echo $response_text;

$data_array=array("HasMore"=>false,"Resources"=>array());
$doc = phpQuery::newDocumentHTML($response_text);
phpQuery::selectDocument($doc);
$trs=pq("table#topic_list tbody tr");
// echo "trs size:".count($trs)."<br>";

foreach ($trs as $tr) {
    $resource_data=parse_list_tr($tr);
    array_push($data_array["Resources"],$resource_data);
}

$data_array["HasMore"]=count(pq("div.nav_title > a:contains('下一頁')"))>0;
$data_array["list_uri"]=$params["list_uri"];

$json_result=json_encode($data_array,JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);
header("Access-Control-Allow-Origin:*");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
echo $json_result;

/*
//https://share.dmhy.org/topics/list?keyword=fate&sort_id=2&team_id=185&order=date-desc
<tr class="">
<!--td 0-->
<td width="98">
2020/04/06 15:53			<span style="display: none;">2020/04/06 15:53</span></td>
<!--td 1-->
<td width="6%" align="center">
<a class="sort-31"
    href="/topics/list/sort_id/31">
    <b><font color=red>季度全集</font></b></a>
    </td>
    <!--td 2-->
    <td class="title">
    
    <span class="tag">
    <a  href="/topics/list/team_id/185" >
    极影字幕社</a></span>
    <a href="/topics/view/538536_10_Fate_Grand_Order_-_Absolute_Demonic_Front_Babylonia_01-21_BIG5_720p_AVC_MP4.html"  target="_blank" >
    【極影字幕社】★10月新番 <span class="keyword">Fate</span> Grand Order - 絕對魔獸戰線巴比倫尼亞 / Absolute Demonic Front Babylonia 第01-21話 合集 BIG5 720p AVC MP4（字幕社招人內詳）</a>
    <span style="color: gray;">約5條評論</span>			</td>
    <!--td 3-->
    <td nowrap="nowrap" align="center"><a class="download-arrow arrow-magnet" title="磁力下載" href="magnet:?xt=urn:btih:DBO7BXLS5MKAAEOV43DJYQYAQQ4GQVOC&dn=&tr=http%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=udp%3A%2F%2F104.238.198.186%3A8000%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=http%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2Ftracker.publicbt.com%3A80%2Fannounce&tr=http%3A%2F%2Ftracker.prq.to%2Fannounce&tr=http%3A%2F%2Fopen.acgtracker.com%3A1096%2Fannounce&tr=https%3A%2F%2Ft-115.rhcloud.com%2Fonly_for_ylbud&tr=http%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=http%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker1.itzmx.com%3A8080%2Fannounce&tr=udp%3A%2F%2Ftracker2.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=udp%3A%2F%2Ftracker4.itzmx.com%3A2710%2Fannounce&tr=http%3A%2F%2F1337.abcvg.info%2Fannounce&tr=http%3A%2F%2F185.83.215.123%3A6969%2Fannounce&tr=http%3A%2F%2F62.210.202.61%2Fannounce&tr=http%3A%2F%2F78.30.254.12%3A2710%2Fannounce&tr=http%3A%2F%2F87.110.238.140%3A6969%2Fannounce&tr=http%3A%2F%2F95.211.168.204%3A2710%2Fannounce&tr=http%3A%2F%2Facg.rip%3A6699%2Fannounce&tr=http%3A%2F%2Fexplodie.org%3A6969%2Fannounce&tr=http%3A%2F%2Fmail2.zelenaya.net%2Fannounce&tr=http%3A%2F%2Fopen.acgnxtracker.com%2Fannounce&tr=http%3A%2F%2Fretracker.sevstar.net%3A2710%2Fannounce&tr=http%3A%2F%2Fshare.camoe.cn%3A8080%2Fannounce&tr=http%3A%2F%2Ft.acg.rip%3A6699%2Fannounce&tr=http%3A%2F%2Ft.nyaatracker.com%2Fannounce&tr=http%3A%2F%2Ftracker.bt4g.com%3A2095%2Fannounce&tr=http%3A%2F%2Ftracker.corpscorp.online%2Fannounce&tr=http%3A%2F%2Ftracker.filepit.to%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker.files.fm%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker.gbitt.info%2Fannounce&tr=http%3A%2F%2Ftracker.lelux.fi%3A80%2Fannounce&tr=http%3A%2F%2Ftracker.moeking.me%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker.nyaa.uk%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker.torrentyorg.pl%2Fannounce&tr=http%3A%2F%2Ftracker.tvunderground.org.ru%3A3218%2Fannounce&tr=http%3A%2F%2Ftracker.uw0.xyz%3A6969%2Fannounce&tr=http%3A%2F%2Ftracker01.loveapp.com%3A6789%2Fannounce&tr=http%3A%2F%2Ftracker3.itzmx.com%3A6961%2Fannounce&tr=http%3A%2F%2Fvps02.net.orel.ru%2Fannounce&tr=https%3A%2F%2F1337.abcvg.info%2Fannounce&tr=https%3A%2F%2F2.tracker.eu.org%2Fannounce&tr=https%3A%2F%2Ftracker.gbitt.info%2Fannounce&tr=https%3A%2F%2Ftracker.nanoha.org%2Fannounce&tr=https%3A%2F%2Ftracker.vectahosting.eu%3A2053%2Fannounce">&nbsp;</a></td>
    <!--td 4-->
    <td nowrap="nowrap" align="center">6.4GB</td>
    <!--td 5-->
    <td nowrap="nowrap" align="center"><span class="btl_1">2</span></td>
    <!--td 6-->
    <td nowrap="nowrap" align="center"><span class="bts_1">0</span></td>
    <!--td 7-->
    <td nowrap="nowrap" align="center">20</td>
    <!--td 8-->
    <td align="center"><a href="/topics/list/user_id/701000">後期秋沫</a></td>
    </tr>
*/    
function parse_list_tr($tr){
    $field_name_array=array("Title","TypeId","TypeName","SubgroupId","SubgroupName","Magnet","PageUrl","FileSize","PublishDate");
    $field_html_array=array();
    $field_data_array=array();
    
    $tds=pq($tr)->find("td");
//     echo "td size:".count($tds);
    foreach ($tds as $td) {
        array_push($field_html_array,$td);
    }
    
    //Title    
    $td2_a_last=pq($field_html_array[2])->find("a")->filter(":last");
    $field_data_array["Title"]=pq($td2_a_last)->text();
    //TypeId,TypeName
    $td1_a0=pq($field_html_array[1])->find("a");
    $field_data_array["TypeId"]=intval(str_replace("/topics/list/sort_id/","",pq($td1_a0)->attr("href")));
    $field_data_array["TypeName"]=pq($td1_a0)->text();
    //SubgroupId,SubgroupName
    $td2_a0=pq($field_html_array[2])->find("a")->filter(":first");
    $field_data_array["SubgroupId"]=intval(str_replace("/topics/list/team_id/","",pq($td2_a0)->attr("href")));
    $field_data_array["SubgroupName"]=pq($td2_a0)->text();
    //Magnet
    $td3_a0=pq($field_html_array[3])->find("a")->filter(":first");
    $field_data_array["Magnet"]=pq($td3_a0)->attr("href");
    //PageUrl 
    $field_data_array["PageUrl"]=$params["host_uri"].pq($td2_a_last)->attr("href");
    //FileSize
    $field_data_array["FileSize"]=pq($field_html_array[4])->text();
    //PublishDate
    $field_data_array["PublishDate"]= date("Y-m-d H:i:s" ,strtotime(pq($field_html_array[0])->find("span")->text()));
    
    foreach($field_name_array as $field_name){
        $field_data_array[$field_name]=trim($field_data_array[$field_name]);
    }
    
    return $field_data_array;
}
?>